package com.slashmanx.goals2u;

import java.util.ArrayList;

import org.json.JSONException;

import com.slashmanx.goals2u.others.Utilities;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class AirtricityTab extends Activity 
{
	public GoalsDatabaseManager db;
	private Intent GoalsIntent;
	private String type;
	public ArrayList<Match> AirMatchList = new ArrayList<Match>();
	public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        db = new GoalsDatabaseManager(this);
        Bundle extras = getIntent().getExtras();
        if(extras !=null) 
        {
        	type = extras.getString("type");
        }
        if(Utilities.isOnline(this))
        {
	        try {
				AirMatchList = Utilities.getScores(db, type, "Airtricity");
			} catch (JSONException e) {
				// TODO Auto-generated catch block
			}
        }
        else
        {
        	AirMatchList =  db.getAllMatches(type, "Airtricity");
        	Toast.makeText(this, "No Internet Connection to Update Scores", Toast.LENGTH_SHORT).show();
        }
        setContentView(R.layout.main);
       
        
        GoalsIntent = new Intent(this, GoalsService.class);
        
        startService(GoalsIntent);
		registerReceiver(broadcastAirTabReceiver, new IntentFilter(GoalsService.BROADCAST_ACTION));
		
		ArrayAdapter<Match> listAdapter = new ArrayAdapter<Match>(this,android.R.layout.simple_list_item_1, AirMatchList);
		
		ListView list = (ListView) findViewById(R.id.listView1);
		
		list.setAdapter((ListAdapter)listAdapter);

    	ArrayAdapter<Match> adapter = (ArrayAdapter<Match>)list.getAdapter();
        
        list.setClickable(true);
		
		list.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// TODO Auto-generated method stub
				Match tmp = AirMatchList.get(position);
				Intent intent = new Intent(AirtricityTab.this, MatchScreen.class);
		    	intent.putExtra("matchID", tmp.getId());
		    
		    	startActivity(intent);
				
			}
		   });
        
        
    }
    
    private BroadcastReceiver broadcastAirTabReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
        	updateUI(intent);       
        }
    };	
    
    public void onResume() {
		super.onResume();		
		startService(GoalsIntent);
		if(db != null)
		{
            db.close();
		}
		db = new GoalsDatabaseManager(this); 
		registerReceiver(broadcastAirTabReceiver, new IntentFilter(GoalsService.BROADCAST_ACTION));
	}
	
	@Override
	public void onPause() {
		super.onPause();		
		stopService(GoalsIntent);
		if(db != null)
		{
			db.close();
		}
		db = null;
		try
		{
			unregisterReceiver(broadcastAirTabReceiver);
		}
		catch(IllegalArgumentException iae)
		{
			// Ignore Exception
		}
	}
	
	public void onDestroy() {
		super.onDestroy();
		stopService(GoalsIntent);
		if(db != null)
		{
			db.close();
		}
		db = null;
		
		try
		{
			unregisterReceiver(broadcastAirTabReceiver);
		}
		catch(IllegalArgumentException iae)
		{
			// Ignore Exception
		}
	}
    
    
    
    private void updateUI(Intent intent) {

        ListView list = (ListView) findViewById(R.id.listView1);
        
    	ArrayAdapter<Match> adapter = (ArrayAdapter<Match>)list.getAdapter();
		update(db, adapter);
    }
    
    public void update(GoalsDatabaseManager db, ArrayAdapter<Match> adapter)
    {
    	try {
        	adapter.clear();
        	AirMatchList = Utilities.getScores(db, type, "Airtricity");
        	for(Match m : AirMatchList)
        	{
        		adapter.add(m);
        	}
        	adapter.notifyDataSetChanged();

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    
    

}
